#include<stdio.h>
#include<stdlib.h>



int *arraylist, length;



void primary_menu();
void newarraylist();
void display();
void appendlist(int );
void extendlist();
void insertlist(int , int );
void removeitem(int );
void popitem();
void countitem(int );
void sortlist();
void reverselist();
void indexitem(int );



void primary_menu()
{
int ch1=1, ch2;
printf("\n\nThis program demostrates python's list operations");
printf("\nin C using dynamic arrays");
while(ch1==1)
{
printf("\n\n\n\n\nPROGRAM MENU");
printf("\n\n1. Input new list");
printf("\n2. Display list");
printf("\n3. Append");
printf("\n4. Extend");
printf("\n5. Insert");
printf("\n6. Remove");
printf("\n7. Pop");
printf("\n8. Count");
printf("\n9. Sort");
printf("\n10. Reverse");
printf("\n11. Index");
printf("\n12. Exit");
printf("\nInput choice : ");
scanf("%d", &ch2);
switch(ch2)
{
case 1 : newarraylist();
	 break;
case 2 : display();
	 break;
case 3 : printf("\nInput value to append at end : ");
	 int val;
	 scanf("%d", &val);
	 appendlist(val);
	 display();
	 break;
case 4 : extendlist();
	 display();
	 break;
case 5 : printf("\nInput value to insert : ");
	 int inval, pos;
	 scanf("%d", &inval);
	 printf("\nPosition to insert : ");
	 scanf("%d", &pos);
	 insertlist(inval, pos);
	 display();
	 break;
case 6 : printf("\nEnter element to remove from list : ");
	 int rmval;
	 scanf("%d", &rmval);
	 removeitem(rmval);
	 display();
	 break;
case 7 : popitem();
	 break;
case 8 : printf("\nEnter value for which occurences have to be found : ");
	 int oval;
	 scanf("%d", &oval);
	 countitem(oval);
	 break;
case 9 : sortlist();
	 display();
	 break;
case 10 : reverselist();
	  display();
	  break;
case 11 : printf("\nEnter element whose index is to be found : ");
	  int indval;
	  scanf("%d", &indval);
	  indexitem(indval);
	  break;
case 12 : ch1=0;
	  break;
default : printf("\n\nWrong input value!!!\n");
	  break;
}
}
}



void newarraylist()
{
printf("\nHow many elements do you wish to enter?");
printf("\nInput value : ");
scanf("%d", &length);
arraylist=malloc((length)*sizeof(int));
int i;
for(i=0;i<length;i++)
{
printf("\nInput integer %d : ", i+1);
scanf("%d", &arraylist[i]);
}
display();
}



void display()
{
int *temp=arraylist;
if(length>0)
{
int i;
printf("\n");
printf("\n\nThe list is :");
for(i=0;i<length;i++)
printf(" %d", temp[i]);
printf("\n");
}
else
printf("\nThe list contains no elements\n");
}



void appendlist(int num)
{
arraylist[length] = (int)malloc(sizeof(int));
arraylist[length] = num;
length = length + 1;
}



void extendlist()
{
int n,i;
printf("\n\nHow many elements to you want to add in the list?");
printf("\nInput value : ");
scanf("%d", &n);
for(i=length;i<(length+n);i++)
{
arraylist[i] = (int)malloc(sizeof(int));
printf("\nEnter element : ");
scanf("%d", &arraylist[i]);

}
length = length + n;
}



void insertlist(int val, int pos)
{
if((pos-1)==length)
{
arraylist[pos-1] = (int)malloc(sizeof(int));
arraylist[pos-1] = val;
length = length + 1;
}
else if((pos-1)<length && (pos-1)>-1)
{
arraylist[pos-1] = val;
}
else
printf("\n\nInput position is incorrect...\n");
}


void removeitem(int num)
{
int flag=0, i=0, j;
while(i<length)
{
if((arraylist[i]==num) && (i<length-1))
{
for(j=i;j<length-1;j++)
arraylist[j] = arraylist[j+1];
length = length - 1;
flag = 1;
break;
}
else if((arraylist[i]==num) && (i==length-1))
{
length = length - 1;
flag = 1;
break;
}
else
flag = 0;
i++;
}
if(flag==1)
{
printf("\n\nElement deleted");
}
else
printf("\n\nElement not in list...!!!\n");
}



void popitem()
{
if(length>0)
length = length - 1;
display();
}



void countitem(int num)
{
int i, cnt=0;
for(i=0;i<length;i++)
{
if(arraylist[i]==num)
cnt++;
}
printf("\nThe number of occurences of %d are : %d\n", num, cnt);
}



void sortlist()
{
int i, j, min, temp;
for(i=0;i<length;i++)
{
min = i;
for(j=i+1;j<length;j++)
{
if(arraylist[j]<arraylist[min])
min = j;
}
if(min!=i)
{
temp=arraylist[i];
arraylist[i]=arraylist[min];
arraylist[min]=temp;
}
}
printf("\nList sorted successfully...");
}



void reverselist()
{
int atemp[length], i=length-1, j=0;
while(i>-1)
atemp[j++]=arraylist[i--];
i=0;
while(i<length)
{
arraylist[i]=atemp[i];
i++;
}
printf("\nList successfully reversed...");
}



void indexitem(int num)
{
int i, pos, flag=0;
for(i=0;i<length;i++)
{
if(arraylist[i]==num)
{
pos=i+1;
flag=1;
break;
}
}
printf("\nElement found at position %d...", pos);
}



void main()
{
primary_menu();
printf("\n");
}
